﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using YogaDictionary.Common;
using YogaDictionary.Model;

namespace YogaDictionary.ViewModel
{
    public class ViewModelWord : ViewModelBase
    {
        private Word _word;

        #region Public Properties

        public RelayCommand<Window> CloseWindowCommand { get; private set; }

        public string WordWindowTitle
        {
            get { return _word.Value; }
        }

        public string PrimaryWords
        {
            get
            {
                return _word.Definition.PrimaryWords.IsNullOrEmpty() ? 
                    "" : _word.Definition.PrimaryWords.ConcatToStringWithSeparator(", ");
            }
        }

        public string SecondaryWords
        {
            get
            {
                return _word.Definition.SecondaryWords.IsNullOrEmpty() ?
                    "" : _word.Definition.SecondaryWords.ConcatToStringWithSeparator(", ");
            }
        }

        public string Description
        {
            get
            {
                return _word.Definition.Descriptions.IsNullOrEmpty() ?
                    "" : _word.Definition.Descriptions.First().Value;
            }
        }

        #endregion

        #region Methods

        public ViewModelWord(Word word)
        {
            if (word == null)
                throw new ArgumentNullException("word");
            _word = word;

            CloseWindowCommand = new RelayCommand<Window>(CloseWindow);
        }

        public void ChangeWord(Word word)
        {
            _word = word;
            RaiseAllWordPropertiesChanged();
        }

        public void CloseWindow(Window window)
        {
            Log.Debug("Close window for: " + _word.Value);
            window.Close();
            ViewModelWindowsManager.RemoveWindowFromTheList(window);
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}

        private void RaiseAllWordPropertiesChanged()
        {
            RaisePropertyChanged("WordWindowTitle");
            RaisePropertyChanged("PrimaryWord");
            RaisePropertyChanged("Description");
            RaisePropertyChanged("Synonims");
        }

        #endregion

    }
}